// 这份代码专门处理逻辑层的操作
const circlePostDao = require('../dao/circlePostDao');

const { createUserJwtToken } = require('../util/server_jwt');

const circlePostService = {
    /*
     * 上传动态图片接口
     */
    async uploadCirclePostImg(uid, postImg) {
        let { buffer, circlePostId, idx } = postImg;
        let result;
        try {
            let res = await circlePostDao.uploadCirclePostImg(uid, buffer, circlePostId, idx);
            if (res.success) {
                result = { success: true, msg: "上传成功"};
            } else {
                result = res;
            }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，发布失败" };
        }
        return result;
    },
    /*
     * 预发布动态接口
     */
    async preAddCirclePost(uid, circlePost) {
        let { title, publishTime, content, circlePostTypeId } = circlePost;
        let result;
        try {
            let res = await circlePostDao.preAddCirclePost(uid, title, publishTime, content, circlePostTypeId);
            result = { success: true, msg: "预发布成功", circlePostId: res.insertId };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，发布失败" };
        }
        return result;
    },
    /*
     * 发布动态
     */
    async addCirclePost(uid, circlePost) {
        let circlePostId = circlePost.id;
        let result;
        try {
            let res = await circlePostDao.addCirclePost(uid, circlePostId);
            result = res;
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，发布失败" };
        }
        return result;
    },
    /*
     * 查看动态详情
     */
    async getPostInfo(uid, circlePost) {
        let postId = circlePost.id;
        let result;
        try {
            let res = await circlePostDao.getPostInfo(uid, postId);
            if (res.length) {
                result = { success: true, msg: "查看动态详情成功", post: res[0] };
            } else {
                result = { success: false, msg: "动态不存在" };
            }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，查看失败" };
        }
        return result;
    },
    /*
     * 删除动态
     */
    async delPost(uid, circlePost) {
        let postId = circlePost.id;
        let result;
        try {
            let res = await circlePostDao.delPost(uid, postId);
            if (res.affectedRows) {
                result = { success: true };
            } else {
                result = { success: false, msg: "动态是你的吗你就删" };
            }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错" };
        }
        return result;
    },
    /*
     * 点赞动态
     */
    async likePost(uid, circlePost) {
        let postId = circlePost.id;
        let result;
        try {
            result = await circlePostDao.likePost(uid, postId);
            if (result.affectedRows) {
                result = { success: true, msg: "点赞成功" };
            } else {
                result = { success: false, msg: "不可重复点赞" };
            }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错" };
        }
        return result;
    },
    /*
     * 取消点赞动态
     */
    async nolikePost(uid, circlePost) {
        let postId = circlePost.id;
        let result;
        try {
            result = await circlePostDao.nolikePost(uid, postId);
            if (result.affectedRows) {
                result = { success: true, msg: "取消点赞成功" };
            } else {
                result = { success: false, msg: "你点赞了吗你就取消点赞" };
            }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错" };
        }
        return result;
    },
    /*
     * 评论动态
     */
    async addCommentPost(uid, postComment) {
        let { postId, time, content } = postComment;
        let result;
        try {
            await circlePostDao.addCommentPost(uid, postId, time, content);
            result = { success: true, msg: "评论成功" };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错" };
        }
        return result;
    },
    /*
     * 删除评论
     */
    async delCommentPost(uid, postComment) {
        let postCommentId = postComment.id;
        let result;
        try {
            let res = await circlePostDao.delCommentPost(uid, postCommentId);
            if (res.affectedRows) {
                result = { success: true, msg: "删除成功" };
            } else {
                result = { success: false, msg: "评论是你的吗你就删" };
            }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错" };
        }
        return result;
    },
    /*
     * 获取推荐动态
     */
    async getRecommendPostList(uid, lastPost) {
        let lastPostId = lastPost.id;
        let result;
        try {
            let res = await circlePostDao.getRecommendPostList(uid, lastPostId);
            result = { success: true, msg: "获取推荐动态成功", data: res };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错" };
        }
        return result;
    },
    /*
     * 获取评论列表
     */
    async getPostCommentList(postCommentRequest) {
        let { postId, lastPostCommentId } = postCommentRequest;
        let result;
        try {
            let res = await circlePostDao.getPostCommentList(postId, lastPostCommentId);
            result = { success: true, msg: '获取评论列表成功', data: res };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错" };
        }
        return result;
    },
    // 获取动态图片预签名URL
    async getCirclePostImgUrl(imgId) {
        let result;
        try {
            let res = await circlePostDao.getCirclePostImgUrl(imgId);
            if (res.success) {
                result = { success: true, msg: '获取图片预签名URL成功', data: res.url };
            } else {
                result = res;
            }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错" };
        }
        return result;
    },
    async getAllCirclePostType() {
        let result;
        try {
            let data = await circlePostDao.getAllCirclePostType();
            // for (let i = 0; i < data.length; i++) {
            //     data[i].active = false;
            // }
            result = { success: true, msg: '获取所有动态类型成功', data };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错" };
        }
        return result;
    }
};

module.exports = circlePostService;